package com.hutao.medical.controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.util.JdbcConnectionTool;
import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.query.FullTextVO;
import com.hutao.medical.service.HospitalNoteService;

@RequestMapping("/fullText")
@Controller
public class FullTextSearchController {

	private static Logger log = LoggerFactory.getLogger(FullTextSearchController.class);

	@Autowired
	private HospitalNoteService hospitalNoteService;
	
	
	@ResponseBody
	@RequestMapping("/fulltextSearchJson")
	public List<Map<String,Object>> fulltextSearchJson(@RequestBody List<FullTextVO> fullTextVo){
		
		
		
		
		return hospitalNoteService.getFullText(fullTextVo);
	}
	
	

	@RequestMapping("/getFullTextSearch")
	public String getFullTextSearch() {
		List<String> fullTextList = hospitalNoteService.getFullTextSearch();
		for (String fullText : fullTextList) {
			Map<String, Object> map = JsonUtils.jsonToPojo(fullText, Map.class);
			System.out.println(map);
		}
		return null;
	}

	@GetMapping("/search")
	public String fullTextSearch(String jsonStr, Map<String, Object> results) {
		List<Map<String, Object>> fullTextMapList = new ArrayList<>();

		if (StringUtils.isNotBlank(jsonStr)) {
			List<String> fullTextSearchList = query(jsonStr);

			if (CollectionUtils.isNotEmpty(fullTextSearchList)) {
				for (String fullText : fullTextSearchList) {
					Map<String, Object> map = JsonUtils.jsonToPojo(fullText, Map.class);
					fullTextMapList.add(map);
					log.info(map.toString());
				}
			}
		}

		results.put("fullTextMapList", fullTextMapList);
		return "query/fullTextSearch";
	}

	public static List<String> query(String jsonStr) {
		String sql = "select hosp_basic_json from hosp_note_basic_json where 1=1 " + jsonStr;
		log.info("sql=" + sql);
		Connection conn = JdbcConnectionTool.getConnection();
		Statement stm = null;
		ResultSet rs = null;
		List<String> fullTextList = new ArrayList<>();
		try {
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);

			while (rs.next()) {
				String hospitalNoteBasic = new String(rs.getString("hosp_basic_json"));
				log.info(hospitalNoteBasic);
				fullTextList.add(hospitalNoteBasic);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcConnectionTool.close(rs, stm, conn);
		}
		return fullTextList;

	}

}
